package com.ldq.imooc.basics.socket;

import java.io.*;
import java.net.Socket;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/23 15:07
 * @Modified by :
 */
public class TCPClient {

    public static void main(String[] args) {

        try {
            //1、创建Socket对象，指定服务器地址和端口号
            Socket socket = new Socket("127.0.0.1", 8888);
            //2、获取字节输出流，向服务器端发送信息
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("用户名：administrator;密码：admin");
            pw.flush();

            socket.shutdownOutput();

            //3、获取输入流，读取服务器响应信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println("client001，Server："+line);
            }
            socket.shutdownInput();

            //4、关闭资源
            br.close();
            is.close();

            pw.close();
            os.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
